package googleJam.qualification2012;

import java.util.HashMap;

import googleJam.problem.CaseTest;

public class Qualification2012CaseTest extends CaseTest{

	private static final String[] ENCODED_TEXTS = {	"ejp mysljylc kd kxveddknmc re jsicpdrysi",
													"rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd",
													"de kr kd eoya kw aej tysr re ujdr lkgc jv"	};

	private static final String[] DECODED_TEXTS = {	"our language is impossible to understand",
													"there are twenty six factorial possibilities",
													"so it is okay if you want to just give up"	};

	private static HashMap<Integer, Character> s_translateMap; 
	
	public Qualification2012CaseTest(String beginLine, int index, int beginIndex) {
		super(beginLine, index, beginIndex);
	}
	
	private String translate(String encodedString)
	{
		StringBuilder builder = new StringBuilder();
		int length = encodedString.length();
		for(int i = 0; i< length; i++)
		{
			builder.append(getSharedTranslateMap().get(Integer.valueOf(encodedString.charAt(i))));
		}
		return builder.toString();
	}
	
	private static HashMap<Integer, Character> getSharedTranslateMap()
	{
		if(s_translateMap == null)
		{
			s_translateMap = new HashMap<Integer, Character>();
			for(int i = 0; i < 3; i++)
			{
				String encoded = ENCODED_TEXTS[i];
				String decoded = DECODED_TEXTS[i];
				for(int j = encoded.length() - 1; j >= 0; j -- )
				{
					Integer codeASII = Integer.valueOf(encoded.charAt(j));
					if(!s_translateMap.containsKey(codeASII))
					{
						s_translateMap.put(codeASII, decoded.charAt(j));
					}
				}
			}
			s_translateMap.put(Integer.valueOf('q'), 'z');
			s_translateMap.put(Integer.valueOf('z'), 'q');
		}
		return s_translateMap;
	}
	
	@Override
	public int calculLineNumber() {
		return 1;
	}

	@Override
	public int getEndIndex() {
		return _beginIndex + getNumberLine() - 1;
	}
	@Override
	public void trait(String line, int index) {
		
	}
	@Override
	public String getResult() {
		return translate(_beginLine);
	}
}
